package com.bdqn.petmalldemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.petmalldemo.common.SystemConstants;
import com.bdqn.petmalldemo.entity.Order;
import com.bdqn.petmalldemo.entity.OrderDetail;
import com.bdqn.petmalldemo.mapper.OrderDetailMapper;
import com.bdqn.petmalldemo.service.OrderDetailService;
import com.bdqn.petmalldemo.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lenovo
 * @since 2020-02-10
 */
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {

    @Resource
    private OrderService orderService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void send(String orderDetailId) {
    //先完成订单详情状态的更新
        OrderDetail orderDetail = baseMapper.selectById(orderDetailId);
        orderDetail.setSendStatus(2);
        baseMapper.updateById(orderDetail);
        //更新相关的订单的状态
        //1.查询当前订单详情对应的订单号查询订单详情
        List<OrderDetail> orderDetailList = baseMapper.selectList(new QueryWrapper<OrderDetail>()
                .eq("order_id",orderDetail.getOrderId()));
        Order order=new Order().setId(orderDetail.getOrderId());
        if (orderDetailList.size()>1) {
            //当前订单详情所属的订单有多件商品,调整相应的订单的状态为部分发货
            order.setStatus(SystemConstants.ORDER_STATUS.PARTIAL_DELIVERY);
        }else if (orderDetailList.size()==1){
            //当前订单详情所属的订单只有一件商品,调整相应的订单的状态为全部发货
            order.setStatus(SystemConstants.ORDER_STATUS.SHIP_ALL);
        }
        orderService.updateById(order);

    }
}
